﻿/********************************************************************************
* Author:	Christos Polydorou	(xristos.polydoroy@gmail.com)					*
* Author:	Aris Michail		(aris.michail@gmail.com)						*
* Date:		22/9/2011															*
********************************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


namespace WebApplication
{

    public partial class PropertyAdd : System.Web.UI.Page
    {
        DataClassesDataContext dc = new DataClassesDataContext();
        Property prop = new Property();

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!Session["isValid"].ToString().Equals("true"))
                    Response.Redirect("login.aspx");
            }
            catch (Exception ex)
            {
                Exception ex2 = ex;
                Response.Redirect("login.aspx");
            }

            if (this.DropNomos.SelectedValue.Equals(""))
            {
                this.DropNomos.DataSource = from nomos in dc.Nomois
                                            orderby nomos.Onoma
                                            select new { Onoma = nomos.Onoma };

                this.DropNomos.DataTextField = "Onoma";
                this.DropNomos.DataValueField = "Onoma";
                this.DropNomos.DataBind();
                this.DropNomos.Items.Insert(0, new ListItem("Νομός", ""));

                this.DropDownList1.Items.Insert(0, new ListItem("Δήμος", ""));
            }
        }


        private void populate()
        {
            //        if (this.DropDownList1.SelectedValue.Equals(""))
            //      {
            this.DropDownList1.DataSource = from dimos in dc.Dimois
                                            where dimos.Nomos == this.DropNomos.SelectedValue
                                            orderby dimos.Onoma
                                            select new { Onoma = dimos.Onoma };
            this.DropDownList1.DataTextField = "Onoma";
            this.DropDownList1.DataValueField = "Onoma";
            this.DropDownList1.DataBind();
            this.DropDownList1.Items.Insert(0, new ListItem("Δήμος", ""));
            //        }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            this.prop.LastUpdatedBy = HttpContext.Current.Session["username"].ToString();
            this.prop.CreatedBy = HttpContext.Current.Session["username"].ToString();
            dc.Properties.InsertOnSubmit(prop);
            dc.SubmitChanges();

            if (Calendar1.SelectedDate.Year == 1)
                prop.SignatureDate = null;
            else
                prop.SignatureDate = Calendar1.SelectedDate;
            if (Calendar2.SelectedDate.Year == 1)
                prop.ReceivedDate = null;
            else
                prop.ReceivedDate = Calendar2.SelectedDate;
            if (!TextBox1.Text.Equals(""))
                try
                {
                    prop.PropertySurface = System.Convert.ToDouble(TextBox1.Text);
                }
                catch (Exception ex)
                {
                    Exception ex2 = ex;
                    HttpContext.Current.Session["message"] = @"Συμπληρώστε το πεδίο ""Επιφάνεια"" μόνο με αριθμούς";
                    return;
                }
            if (!DropNomos.SelectedValue.Equals(""))
                prop.PropertyNomos = DropNomos.SelectedItem.Value.ToString();
            if (!DropDownList1.SelectedValue.Equals(""))
                prop.PropertyDimos = DropDownList1.SelectedItem.Value.ToString();
            prop.PropertyArea = TextBox4.Text;
            if (!TextBox5.Text.Equals(""))
                try
                {

                    prop.OSDENumber = long.Parse(TextBox5.Text);
                }
                catch (Exception ex)
                {
                    Exception ex2 = ex;
                    HttpContext.Current.Session["message"] = @"Συμπληρώστε το πεδίο ""Αριθμός ΟΣΔΕ"" μόνο με αριθμούς";
                    return;
                }

            if (!TextBox6.Text.Equals(""))
                try
                {
                    prop.AnadasmosNumber = long.Parse(TextBox6.Text);
                }
                catch (Exception ex)
                {
                    Exception ex2 = ex;
                    HttpContext.Current.Session["message"] = @"Συμπληρώστε το πεδίο ""Αριθμός Αναδασμού"" μόνο με αριθμούς";
                    return;
                }
            prop.RegistrationOffice = TextBox7.Text;
            prop.OwnerName = TextBox8.Text;
            prop.OwnerAddress = TextBox9.Text;
            prop.OwnerTF = TextBox10.Text;
            if (Calendar3.SelectedDate.Year == 1)
                prop.FirstSeedDate = null;
            else
                prop.FirstSeedDate = Calendar3.SelectedDate;

            if (!this.TextBox11.Text.Equals(""))
                try
                {
                    prop.FirstSeedSeed = double.Parse(TextBox11.Text);
                }
                catch (Exception ex)
                {
                    Exception ex2 = ex;
                    HttpContext.Current.Session["message"] = @"Συμπληρώστε το πεδίο ""1η Σπορά-Πρώτος Σπόρος"" μόνο με αριθμούς";
                    return;
                }

            if (Calendar4.SelectedDate.Year == 1)
                prop.SecondSeedDate = null;
            else
                prop.SecondSeedDate = Calendar4.SelectedDate;

            if (!this.TextBox12.Text.Equals(""))
                try
                {
                    prop.SecondSeedSeed = double.Parse(TextBox12.Text);
                }
                catch (Exception ex)
                {
                    Exception ex2 = ex;
                    HttpContext.Current.Session["message"] = @"Συμπληρώστε το πεδίο ""2η Σπορά-Σπόρος"" μόνο με αριθμούς";
                    return;
                }

            prop.Apopsigeoponou = TextBox13.Text;
            if (Calendar5.SelectedDate.Year == 1)
                prop.ApopsigeoponouDate = null;
            else
                prop.ApopsigeoponouDate = Calendar5.SelectedDate;
            prop.FirstSeedKodikosSporou = SeedCode1.Text;
            prop.SecondSeedKodikosSporou = SeedCode2.Text;
            prop.Coordinates = Coordinates.Text;            
            prop.CreatedBy = (string)Session["username"];
            prop.LastUpdatedBy = (string)Session["username"];

            CreateProtocolNumber();

            try
            {
                dc.SubmitChanges();
            }
            catch (Exception ex)
            {
                Exception ex2 = ex;
                HttpContext.Current.Session["message"] = "Η προσθήκη του ακινήτου απέτυχε";
            }

            HttpContext.Current.Session["message"] = "Το ακίνητο προστέθηκε επιτυχώς";
            Response.Redirect("default.aspx");
        }
        protected void Button2_Click(object sender, EventArgs e)
        {
            this.Button1_Click(sender, e);
        }
        protected void DropNomos_SelectedIndexChanged(object sender, EventArgs e)
        {
            populate();
        }

        private void CreateProtocolNumber()
        {
            // If all fields are filled, create the protocol number

            if ((!this.Calendar1.SelectedDate.ToString().Equals("")) &&
                (!this.Calendar2.SelectedDate.ToString().Equals("")) &&
                (!this.Calendar3.SelectedDate.ToString().Equals("")) &&
                (!this.Calendar4.SelectedDate.ToString().Equals("")) &&
                (!this.Calendar5.SelectedDate.ToString().Equals("")) &&
                (!this.TextBox1.Text.Equals("")) &&
                (!this.TextBox4.Text.Equals("")) &&
                (!this.TextBox5.Text.Equals("")) &&
                (!this.TextBox6.Text.Equals("")) &&
                (!this.TextBox7.Text.Equals("")) &&
                (!this.TextBox8.Text.Equals("")) &&
                (!this.TextBox9.Text.Equals("")) &&
                (!this.TextBox10.Text.Equals("")) &&
                (!this.TextBox11.Text.Equals("")) &&
                (!this.TextBox12.Text.Equals("")) &&
                (!this.TextBox13.Text.Equals("")) &&
                (!this.SeedCode1.Text.Equals("")) &&
                (!this.SeedCode2.Text.Equals("")) &&
                (!this.Coordinates.Text.Equals("")) &&
                (!this.DropDownList1.SelectedItem.Value.Equals("")) &&
                (!this.DropNomos.SelectedItem.Value.Equals(""))
            )
                this.prop.ProtocolNumber = this.prop.ID.ToString() + this.TextBox7.Text.Substring(0, 3);
        }

    }
}